package com.bsuir.avareader.adapters;

import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.bsuir.avareader.OpenFileActivity;
import com.bsuir.avareader.R;

public class FileAdapter extends BaseAdapter {

	private LayoutInflater inflater;
	private List<String> items = new ArrayList<String>();

	public FileAdapter(Context context) {
		inflater = LayoutInflater.from(context);		
	}

	public void setItems(List<String> items) {
		this.items = items;
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return items.size();
	}

	@Override
	public Object getItem(int position) {
		return items.get(position);
	} 

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			View view = inflater.inflate(R.layout.open_file_list, parent, false);
			ViewHolder holder = getHolder(view);
			view.setTag(holder);
			convertView = view;
		}
		ViewHolder holder = (ViewHolder) convertView.getTag();
		holder.textView.setText(items.get(position));
		holder.imageView.setImageResource(getImageId(position));
		return convertView;
	}

	private ViewHolder getHolder(View view) {
		ViewHolder holder = new ViewHolder();
		holder.imageView = (ImageView) view.findViewById(R.id.image);
		holder.textView = (TextView) view.findViewById(R.id.text);
		return holder;
	}

	private int getImageId(int position) {
		if (OpenFileActivity.fileManager.itemIsDirectory(items.get(position))) {
			return R.drawable.ic_folder;
		} else {
			
			return R.drawable.ic_book;
		}	  
	}

	class ViewHolder {
		public ImageView imageView;
		public TextView textView;
	}

}
